package com.bnz.page.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bnz.page.entiity.TbGoods;
import com.bnz.page.entiity.TbGoodsDesc;
import com.bnz.page.entiity.TbItem;
import com.bnz.page.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class PageServiceImpl implements PageService {
    //载入模板引擎
    @Autowired
    private SpringTemplateEngine engine;
    @Autowired
    private TbGoodsService goodsService;
    @Autowired
    private TbGoodsDescService goodsDescService;
    @Autowired
    private TbItemCatService itemCatService;
    @Autowired
    private TbItemService itemService;
    @Override
    public void createHtml(long id) throws IOException {
        //定义上下文模板的对象
        Context context = new Context();
        //定义模型 数据的map集合
        Map<String, Object> dataMap = new HashMap<>();
        //1、根据spu商品id查询商品
        TbGoods goods = goodsService.getById(id);
        //将商品放到dataMap集合中
        dataMap.put("goods",goods);
        //2、根据商品id查询商品描述表。
        TbGoodsDesc goodsDesc = goodsDescService.getById(id);
        //将描述信息放到map中
        dataMap.put("goodsDesc",goodsDesc);
        //3、查询商评分类  分为一级，二级，三级。
        String category1Name = itemCatService.getById(goods.getCategory1Id()).getName();
        String category2Name = itemCatService.getById(goods.getCategory2Id()).getName();
        String category3Name = itemCatService.getById(goods.getCategory3Id()).getName();
        //将上面的三级分类放到dataMap中
        dataMap.put("category1Name",category1Name);
        dataMap.put("category2Name",category2Name);
        dataMap.put("category3Name",category3Name);
        //将ItemImages上传的商品图片放到dataMap
        dataMap.put("itemImages", JSON.parseArray(goodsDesc.getItemImages(),Map.class));
        //将customAttributeItems自定义扩展属性的值放到dataMap
        dataMap.put("customAttributeItems",JSON.parseArray(goodsDesc.getCustomAttributeItems(), Map.class));
        //将specificationItems规格属性放到dataMap
        dataMap.put("specificationItems",JSON.parseArray(goodsDesc.getSpecificationItems(),Map.class));
        //根据传入的spu商品id，查询出对应的所有的sku商品列表，可以按照is_default字段降序排序，我们默认显示的应该就是第一条
        List<TbItem> items = itemService.list(new QueryWrapper<TbItem>().eq("goods_id", id).orderByDesc("is_default"));
        //得到上面items中第一条记录，就是默认显示在页面中的哪条数据,将数据都放到dataMap中
        //默认为第一条，
        System.out.println("items = " + items);

        dataMap.put("items",items);
        dataMap.put("item",items.get(0));
        //map放入上下文路径中
        context.setVariables(dataMap);
        FileWriter fileWriter =
                new FileWriter("D:\\迅雷下载\\NginxWindows_V1.21.6_XiTongZhiJia\\nginx-1.21.6\\html\\"+id+".html");
        //模板中的process方法！
        engine.process("item",context,fileWriter);
    }
}
